package com.apollo.demo.controller;

import java.util.Random;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import com.apollo.demo.model.User;

/**
 * Swagger接口示例类
 */
@Api(value = "用户模块", tags = "用户基础信息接口模块")
@RestController
@RequestMapping("/user")
@ResponseBody
public class UserController {

    /**
     * 一个路径参数的接口示例
     */
    @ApiOperation(value = "查询用户信息", notes = "根据用户id查询用户信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "getUserById/{id}")
    public User getUserById(@PathVariable(name = "id") String id) {
        User user = new User();
        user.setId(String.valueOf(new Random().nextInt(1000)));
        user.setName("user" + id);
        user.setSex(true);
        user.setAge(new Random().nextInt(30));
        return user;
    }

    /**
     * 两个请求参数的接口示例
     */
    @ApiOperation(value = "查询用户信息", notes = "根据用户名和性别查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sex", value = "性别", required = true, dataType = "Boolean")
    })
    @GetMapping(value = "/getUserByNameAndSex")
    public User getUserByNameAndSex(@RequestParam(name = "name") String name, @RequestParam(name = "sex") Boolean sex) {
        User user = new User();
        user.setId(String.valueOf(new Random().nextInt(1000)));
        user.setName(name);
        user.setSex(sex);
        user.setAge(new Random().nextInt(30));
        return user;
    }

    /**
     * 一个请求对象的接口示例
     */
    @ApiOperation(value = "查询用户信息", notes = "根据用户的多项信息查询用户信息")
    @ApiImplicitParam(name = "user", value = "用户对象", required = true, dataType = "User")
    @PostMapping(value = "/getUser")
    public User getUser(@RequestBody User user) {
        User user1 = new User();
        user1.setId(String.valueOf(new Random().nextInt(1000)));
        user1.setName(user.getName());
        user1.setSex(user.getSex());
        user1.setAge(new Random().nextInt(30));
        return user1;
    }

}